//package com.demo.config;
//
//
//import com.demo.constant.Constant;
//import com.demo.util.SecurityUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.security.access.ConfigAttribute;
//import org.springframework.security.access.SecurityConfig;
//import org.springframework.security.authentication.AccountExpiredException;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.web.FilterInvocation;
//import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
//import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
//import org.springframework.stereotype.Component;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Collection;
//import java.util.List;
//import java.util.Map;
//
///**
// * @author lirong
// * @ClassName: UrlFilterInvocationSecurityMetadataSource
// * @Description: 获取访问此URL所需要的角色集和
// * @date 2019-07-10 14:36
// * 自定义权限拦截过滤器
// */
//@Component("urlFilterInvocationSecurityMetadataSource")
//@Slf4j
//public class UrlFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//
//
//    @Override
//    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
//        HttpServletRequest request = ((FilterInvocation) o).getHttpRequest();
//        HttpServletResponse response = ((FilterInvocation) o).getHttpResponse();
//
//        /**
//         * 获取Redis中用户的登录信息
//         * 在SecurityContextHolder中存储了当前与系统交互的用户的信息。Spring Security使用一个Authentication 对象来表示这些信息
//         */
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        String username = (String) authentication.getPrincipal();
//        List<Map<String, String[]>> menuMap = (List<Map<String, String[]>>) redisTemplate.opsForValue().get(Constant.REDIS_PERM_KEY_PREFIX + username);
//        log.info("上下文中获取到的username---"+username+"---9999端口获取到redis中用户信息--："+menuMap);
//
//        // 判断此用户是否登录，是否在其他客户端退出
//        if(null == menuMap){ throw new AccountExpiredException("用户已在其他客户端退出或未登录"); }
//
//        //如用户信息不为空，获取用户权限
//        List<ConfigAttribute> configAttributes = null;
//        for (Map<String, String[]> map : menuMap) {
//            for (String url : map.keySet()) {
//                String[] split = url.split(":");
//                AntPathRequestMatcher antPathMatcher = new AntPathRequestMatcher(split[1], split[0]);
//                if(antPathMatcher.matches(request)){
//                    configAttributes = SecurityConfig.createList(map.get(url));
//                    log.info("返回的权限合集："+configAttributes);
//                    return configAttributes;
//                }
//            }
//        }
//        //没有匹配上的资源，都是登录访问
//        configAttributes = SecurityConfig.createList("ROLE_LOGIN");
//        log.info("未匹配到资源信息--"+configAttributes);
//        return configAttributes;
//    }
//
//    @Override
//    public Collection<ConfigAttribute> getAllConfigAttributes() {
//        return null;
//    }
//
//    @Override
//    public boolean supports(Class<?> aClass) {
//        return false;
//    }
//}
